package com.offcn.service.impl;

import com.alibaba.fastjson.JSON;
import com.netflix.discovery.converters.Auto;
import com.offcn.entity.Result;
import com.offcn.sellergood.entity.GoodsEntity;
import com.offcn.sellergood.feign.GoodsFeign;
import com.offcn.sellergood.feign.ItemCatFeign;
import com.offcn.sellergood.pojo.Goods;
import com.offcn.sellergood.pojo.GoodsDesc;
import com.offcn.sellergood.pojo.Item;
import com.offcn.sellergood.pojo.ItemCat;
import com.offcn.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private GoodsFeign goodsFeign;

    @Autowired
    private ItemCatFeign itemCatFeign;

    @Value("${pagePath}")
    private String pagePath;

    @Autowired
    private TemplateEngine templateEngine;

    private Map<String,Object> getData(Long goodsId){
        Map<String, Object> resultMap = new HashMap<>();
        GoodsEntity goodsEntity = goodsFeign.findById(goodsId).getData();
        Goods tbGoods = goodsEntity.getTbGoods();
        GoodsDesc tbGoodsDesc = goodsEntity.getTbGoodsDesc();
        List<Item> itemList = goodsEntity.getItemList();
        resultMap.put("tbGoods",tbGoods);
        resultMap.put("tbGoodsDesc",tbGoodsDesc);
        resultMap.put("itemList",itemList);
        List<Map> images = JSON.parseArray(tbGoodsDesc.getItemImages(), Map.class);
        resultMap.put("images",images);
        List<Map> specItems = JSON.parseArray(tbGoodsDesc.getSpecificationItems(), Map.class);
        resultMap.put("specs",specItems);
        // 三级分类的数据
        ItemCat itemCat1 = itemCatFeign.findById(tbGoods.getCategory1Id()).getData();
        ItemCat itemCat2 = itemCatFeign.findById(tbGoods.getCategory2Id()).getData();
        ItemCat itemCat3 = itemCatFeign.findById(tbGoods.getCategory3Id()).getData();
        resultMap.put("category1",itemCat1);
        resultMap.put("category2",itemCat2);
        resultMap.put("category3",itemCat3);
        return resultMap;
    }

    @Override
    public void createPageHtml(Long goodsId) throws FileNotFoundException, UnsupportedEncodingException {
        // 需要的数据
        Map<String, Object> data = getData(goodsId);
        // 上下文环境
        Context context = new Context();
        context.setVariables(data);
        // 生成文件
        File dir = new File(pagePath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(dir, goodsId + ".html");

        PrintWriter writer = new PrintWriter(file, "utf-8");
            // 根据模板生成文件
        templateEngine.process("item",context,writer);
        writer.close();
    }
}
